package util;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.*;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HttpClientUtil {
    private static final CloseableHttpClient httpClient;
    private static final Map<String,String> headers=new HashMap<>();
    private static final Logger logger= LoggerFactory.getLogger(HttpClientUtil.class);
    static {
        httpClient= HttpClientBuilder.create().build();
    }
    //构造url
    public static String build_url(String url, Map<String,String> query_path) throws URISyntaxException {
        URIBuilder uriBuilder=new URIBuilder(url);
        if(query_path!=null) {
            for (String key : query_path.keySet()) {
                uriBuilder.addParameter(key, query_path.get(key));
            }
        }
        return uriBuilder.build().toString();
    }
    //构造响应数据
    public static  ResponseData build_response(CloseableHttpResponse response) throws IOException {
        ResponseData responseData=new ResponseData();
        responseData.setCode(response.getStatusLine().getStatusCode());
        String data= EntityUtils.toString(response.getEntity());
        logger.info("响应体数据"+"data={}",data);
        if(data!=null && data.startsWith("{")) {
            responseData.setBody(JSONObject.parseObject(data));
        }
        return responseData;
    }
    //构造响应头
    public static void build_header(String header_key,String header_value){
        headers.put(header_key,header_value);
    }
    //构造get请求
    public static ResponseData get(String url,Map<String,String> query_path) throws URISyntaxException, IOException {
        String new_url=build_url(url, query_path);
        logger.info("new_url={}",new_url);
        HttpGet httpGet=new HttpGet(new_url);
        for(String key: headers.keySet()){
            httpGet.setHeader(key, headers.get(key));
        }
        CloseableHttpResponse response= httpClient.execute(httpGet);
        ResponseData responseData=build_response(response);
        response.close();
        return responseData;
    }
    public static ResponseData get(String url) throws IOException, URISyntaxException {
        return get(url, null);
    }
    //封装post--form
    public static ResponseData post_form(String url,Map<String,String> query_path,Map<String,String> data_form) throws URISyntaxException, IOException {
        String new_url=build_url(url, query_path);
        logger.info("new_url={}",new_url);
        HttpPost httpPost=new HttpPost(new_url);
        //
        httpPost.setHeader("Connection","close");
        for(String key: headers.keySet()){
            httpPost.setHeader(key, headers.get(key));
        }
        List<NameValuePair> data=new ArrayList<>();
        for(String key: data_form.keySet()){
            data.add(new BasicNameValuePair(key, data_form.get(key)));
        }
        logger.info("请求头"+"headers={}",headers);
        logger.info("请求体"+"data={}",data);
        httpPost.setEntity(new UrlEncodedFormEntity(data,"UTF-8"));
        CloseableHttpResponse response= httpClient.execute(httpPost);
        ResponseData responseData=build_response(response);
        response.close();
        return responseData;
    }
    public static ResponseData post_form(String url,Map<String,String> data_form) throws IOException, URISyntaxException {
        return post_form(url,null,data_form);
    }
    //封装post-json
    public static  ResponseData post_json(String url,Map<String,String> query_path,Map<String,Object> data_json) throws URISyntaxException, IOException {
        String new_url=build_url(url, query_path);
        logger.info("new_url={}",new_url);
        HttpPost httpPost=new HttpPost(new_url);
        for(String key: headers.keySet()){
            httpPost.setHeader(key, headers.get(key));
        }
        logger.info("请求头"+"headers={}",headers);
        String data=JSONObject.toJSONString(data_json);
        logger.info("请求体"+"data={}",data);
        httpPost.setEntity(new StringEntity(data, ContentType.APPLICATION_JSON));
        CloseableHttpResponse response= httpClient.execute(httpPost);
        logger.info("");
        ResponseData responseData = build_response(response);
        response.close();
        return responseData;
    }
    public static ResponseData post_json(String url,Map<String,Object> data_json) throws IOException, URISyntaxException {
        return post_json(url, null,data_json);
    }
    public static ResponseData put_json(String url,Map<String,String> query_path,Map<String,Object> data_json) throws URISyntaxException, IOException {
        String new_url=build_url(url, query_path);
        logger.info("new_url={}",new_url);
        HttpPut httpPut=new HttpPut(new_url);
        for(String key: headers.keySet()){
            httpPut.setHeader(key, headers.get(key));
        }
        logger.info("请求头"+"headers={}",headers);
        String data=JSONObject.toJSONString(data_json);
        logger.info("请求体"+"data={}",data);
        httpPut.setEntity(new StringEntity(data,ContentType.APPLICATION_JSON));
        CloseableHttpResponse response= httpClient.execute(httpPut);
        ResponseData responseData = build_response(response);
        response.close();
        return responseData;
    }

    public static ResponseData put_json(String url,Map<String,Object> data_json) throws IOException, URISyntaxException {
        return put_json(url, null,data_json);
    }
    public static ResponseData put_form(String url,Map<String,String> query_path,Map<String,String> data_form) throws URISyntaxException, IOException {
        String build_url = build_url(url, query_path);
        HttpPut httpPut=new HttpPut(build_url);
        for(String key: headers.keySet()){
            httpPut.setHeader(key, headers.get(key));
        }
        List<NameValuePair> data=new ArrayList<>();
        for(String key: data_form.keySet()){
            data.add(new BasicNameValuePair(key, data_form.get(key)));
        }
        logger.info("请求头"+"headers={}",headers);
        logger.info("请求体"+"data={}",data);
        httpPut.setEntity(new UrlEncodedFormEntity(data,"utf-8"));
        CloseableHttpResponse response= httpClient.execute(httpPut);
        ResponseData responseData = build_response(response);
        response.close();
        return responseData;
    }
    public static ResponseData put_form(String url,Map<String,String> data_form) throws IOException, URISyntaxException {
        return put_form(url,null,data_form);
    }
    public static ResponseData delete(String url,Map<String,String> query_path) throws URISyntaxException, IOException {
        String build_url = build_url(url, query_path);
        HttpDelete httpDelete=new HttpDelete(build_url);
        for(String key: headers.keySet()){
            httpDelete.setHeader(key, headers.get(key));
        }
        CloseableHttpResponse response= httpClient.execute(httpDelete);
        ResponseData responseData = build_response(response);
        response.close();
        return responseData;
    }
    public static ResponseData delete(String url) throws IOException, URISyntaxException {
        return delete(url, null);
    }
}
